/*
 Copyright 2011 Abdulla Abdurakhmanov
 Original sources are available at https://code.google.com/p/x2js/

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/*
 Attribute quotation -> ' or "
 Null end tag can be used.
 jQuery.extend
 Changed by Masatsugu Aida 2012
*/

;(function($, undefined){
function X2JS() {
﻿  var VERSION = "1.0.9.3j"

﻿  var quot = "single";
﻿  var quotation = {
﻿  ﻿  "single": "'",
﻿  ﻿  "double": '"'
﻿  };
﻿  var NET = false;

﻿  var DOMNodeTypes = {
﻿  ﻿  ELEMENT_NODE ﻿     : 1,
﻿  ﻿  TEXT_NODE    ﻿     : 3,
﻿  ﻿  CDATA_SECTION_NODE : 4,
﻿  ﻿  DOCUMENT_NODE ﻿     : 9
﻿  }
﻿  
﻿  function getNodeLocalName( node ) {
﻿  ﻿  var nodeLocalName = node.localName;﻿  ﻿  ﻿  
﻿  ﻿  if(nodeLocalName == null) // Yeah, this is IE!!
﻿  ﻿  ﻿  nodeLocalName = node.baseName;
﻿  ﻿  if(nodeLocalName == null || nodeLocalName=="") // =="" is IE too
﻿  ﻿  ﻿  nodeLocalName = node.nodeName;
﻿  ﻿  return nodeLocalName;
﻿  }
﻿  
﻿  function getNodePrefix(node) {
﻿  ﻿  return node.prefix;
﻿  }

﻿  function parseDOMChildren( node ) {
﻿  ﻿  if(node.nodeType == DOMNodeTypes.DOCUMENT_NODE) {
﻿  ﻿  ﻿  var result = new Object;
﻿  ﻿  ﻿  var child = node.firstChild;
﻿  ﻿  ﻿  var childName = getNodeLocalName(child);
﻿  ﻿  ﻿  result[childName] = parseDOMChildren(child);
﻿  ﻿  ﻿  return result;
﻿  ﻿  }
﻿  ﻿  else
﻿  ﻿  if(node.nodeType == DOMNodeTypes.ELEMENT_NODE) {
﻿  ﻿  ﻿  var result = new Object;
﻿  ﻿  ﻿  result.__cnt=0;
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  var nodeChildren = node.childNodes;
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  // Children nodes
﻿  ﻿  ﻿  for(var cidx=0; cidx <nodeChildren.length; cidx++) {
﻿  ﻿  ﻿  ﻿  var child = nodeChildren.item(cidx); // nodeChildren[cidx];
﻿  ﻿  ﻿  ﻿  var childName = getNodeLocalName(child);
﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  result.__cnt++;
﻿  ﻿  ﻿  ﻿  if(result[childName] == null) {
﻿  ﻿  ﻿  ﻿  ﻿  result[childName] = parseDOMChildren(child);
﻿  ﻿  ﻿  ﻿  ﻿  result[childName+"_asArray"] = new Array(1);
﻿  ﻿  ﻿  ﻿  ﻿  result[childName+"_asArray"][0] = result[childName];
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  if(result[childName] != null) {
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  if( !(result[childName] instanceof Array)) {
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  var tmpObj = result[childName];
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result[childName] = new Array();
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result[childName][0] = tmpObj;
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result[childName+"_asArray"] = result[childName];
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  ﻿  var aridx = 0;
﻿  ﻿  ﻿  ﻿  ﻿  while(result[childName][aridx]!=null) aridx++;
﻿  ﻿  ﻿  ﻿  ﻿  (result[childName])[aridx] = parseDOMChildren(child);
﻿  ﻿  ﻿  ﻿  }﻿  ﻿  ﻿  
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  // Attributes
﻿  ﻿  ﻿  for(var aidx=0; aidx <node.attributes.length; aidx++) {
﻿  ﻿  ﻿  ﻿  var attr = node.attributes.item(aidx); // [aidx];
﻿  ﻿  ﻿  ﻿  result.__cnt++;
﻿  ﻿  ﻿  ﻿  result["_"+attr.name]=attr.value;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  // Node namespace prefix
﻿  ﻿  ﻿  var nodePrefix = getNodePrefix(node);
﻿  ﻿  ﻿  if(nodePrefix!=null && nodePrefix!="") {
﻿  ﻿  ﻿  ﻿  result.__cnt++;
﻿  ﻿  ﻿  ﻿  result.__prefix=nodePrefix;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  if( result.__cnt == 1 && result["#text"]!=null  ) {
﻿  ﻿  ﻿  ﻿  result = result["#text"];
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  if(result["#text"]!=null) {
﻿  ﻿  ﻿  ﻿  result.__text = result["#text"];
﻿  ﻿  ﻿  ﻿  delete result["#text"];
﻿  ﻿  ﻿  ﻿  delete result["#text_asArray"];
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  if(result["#cdata-section"]!=null) {
﻿  ﻿  ﻿  ﻿  result.__cdata = result["#cdata-section"];
﻿  ﻿  ﻿  ﻿  delete result["#cdata-section"];
﻿  ﻿  ﻿  ﻿  delete result["#cdata-section_asArray"];
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  
﻿  ﻿  ﻿  if(result.__text!=null || result.__cdata!=null) {
﻿  ﻿  ﻿  ﻿  result.toString = function() {
﻿  ﻿  ﻿  ﻿  ﻿  return (this.__text!=null? this.__text:'')+( this.__cdata!=null ? this.__cdata:'');
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  return result;
﻿  ﻿  }
﻿  ﻿  else
﻿  ﻿  if(node.nodeType == DOMNodeTypes.TEXT_NODE || node.nodeType == DOMNodeTypes.CDATA_SECTION_NODE) {
﻿  ﻿  ﻿  return node.nodeValue;
﻿  ﻿  }﻿  
﻿  }
﻿  
﻿  function startTag(jsonObj, element, attrList, closed) {
﻿  ﻿  var resultStr = "<"+ (jsonObj.__prefix!=null? (jsonObj.__prefix+":"):"") + element;
﻿  ﻿  if(attrList!=null) {
﻿  ﻿  ﻿  for(var aidx = 0; aidx < attrList.length; aidx++) {
﻿  ﻿  ﻿  ﻿  var attrName = attrList[aidx];
﻿  ﻿  ﻿  ﻿  var attrVal = jsonObj[attrName];
﻿  ﻿  ﻿  ﻿  resultStr+=" "+attrName.substr(1)+"="+quotation[quot]+attrVal+quotation[quot];
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  if(!closed)
﻿  ﻿  ﻿  resultStr+=">";
﻿  ﻿  else
﻿  ﻿  ﻿  resultStr+="/>";
﻿  ﻿  return resultStr;
﻿  }
﻿  
﻿  function endTag(jsonObj,elementName) {
﻿  ﻿  return "</"+ (jsonObj.__prefix!=null? (jsonObj.__prefix+":"):"")+elementName+">";
﻿  }
﻿  
﻿  function endsWith(str, suffix) {
﻿      return str.indexOf(suffix, str.length - suffix.length) !== -1;
﻿  }
﻿  
﻿  function jsonXmlSpecialElem ( jsonObj, jsonObjField ) {
﻿  ﻿  if(endsWith(jsonObjField.toString(),("_asArray"))
﻿  ﻿  ﻿  ﻿  || jsonObjField.toString().indexOf("_")==0
﻿  ﻿  ﻿  ﻿  || (jsonObj[jsonObjField] instanceof Function) )
﻿  ﻿  ﻿  return true;
﻿  ﻿  else
﻿  ﻿  ﻿  return false;
﻿  }
﻿  
﻿  function jsonXmlElemCount ( jsonObj ) {
﻿  ﻿  var elementsCnt = 0;
﻿  ﻿  if(jsonObj instanceof Object ) {
﻿  ﻿  ﻿  for( var it in jsonObj  ) {
﻿  ﻿  ﻿  ﻿  if(jsonXmlSpecialElem ( jsonObj, it) )
﻿  ﻿  ﻿  ﻿  ﻿  continue;﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  elementsCnt++;
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  return elementsCnt;
﻿  }
﻿  
﻿  function parseJSONAttributes ( jsonObj ) {
﻿  ﻿  var attrList = [];
﻿  ﻿  var cnt = 0;
﻿  ﻿  if(jsonObj instanceof Object ) {
﻿  ﻿  ﻿  for( var ait in jsonObj  ) {
﻿  ﻿  ﻿  ﻿  if(ait.toString().indexOf("__")== -1 && ait.toString().indexOf("_")==0) {
﻿  ﻿  ﻿  ﻿  ﻿  attrList.push(ait);
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  else if (ait.toString().indexOf("__prefix")== -1) {
﻿  ﻿  ﻿  ﻿  ﻿  ++cnt;
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  return {
﻿  ﻿  ﻿  list: attrList,
﻿  ﻿  ﻿  nonAttr: cnt
﻿  ﻿  };
﻿  }
﻿  
﻿  function parseJSONTextAttrs ( jsonTxtObj ) {
﻿  ﻿  var result ="";
﻿  ﻿  
﻿  ﻿  if(jsonTxtObj.__cdata!=null) {﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  result+="<![CDATA["+jsonTxtObj.__cdata+"]]>";﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  }
﻿  ﻿  
﻿  ﻿  if(jsonTxtObj.__text!=null) {
﻿  ﻿  ﻿  result+=jsonTxtObj.__text;
﻿  ﻿  }
﻿  ﻿  return result
﻿  }
﻿  
﻿  function parseJSONTextObject ( jsonTxtObj ) {
﻿  ﻿  var result ="";

﻿  ﻿  if( jsonTxtObj instanceof Object ) {
﻿  ﻿  ﻿  result+=parseJSONTextAttrs ( jsonTxtObj )
﻿  ﻿  }
﻿  ﻿  else
﻿  ﻿  ﻿  if(jsonTxtObj!=null)
﻿  ﻿  ﻿  ﻿  result+=jsonTxtObj;
﻿  ﻿  
﻿  ﻿  return result;
﻿  }
﻿  
﻿  function parseJSONArray ( jsonArrRoot, jsonArrObj, attrList ) {
﻿  ﻿  var result = "";
﻿  ﻿  if(jsonArrRoot.length == 0) {
﻿  ﻿  ﻿  result+=startTag(jsonArrRoot, jsonArrObj, attrList, true);
﻿  ﻿  }
﻿  ﻿  else {
﻿  ﻿  ﻿  for(var arIdx = 0; arIdx < jsonArrRoot.length; arIdx++) {
﻿  ﻿  ﻿  ﻿  var ret = parseJSONAttributes(jsonArrRoot[arIdx]);
﻿  ﻿  ﻿  ﻿  if (!NET || ret.nonAttr) {
﻿  ﻿  ﻿  ﻿  ﻿  result+=startTag(jsonArrRoot[arIdx], jsonArrObj, ret.list, false);
﻿  ﻿  ﻿  ﻿  ﻿  result+=parseJSONObject(jsonArrRoot[arIdx]);
﻿  ﻿  ﻿  ﻿  ﻿  result+=endTag(jsonArrRoot[arIdx],jsonArrObj);﻿  ﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  result+=startTag(jsonArrRoot[arIdx], jsonArrObj, ret.list, true);
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  return result;
﻿  }
﻿  
﻿  function parseJSONObject ( jsonObj ) {
﻿  ﻿  var result = "";﻿  

﻿  ﻿  var elementsCnt = jsonXmlElemCount ( jsonObj );
﻿  ﻿  
﻿  ﻿  if(elementsCnt > 0) {
﻿  ﻿  ﻿  for( var it in jsonObj ) {
﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  if(jsonXmlSpecialElem ( jsonObj, it) )
﻿  ﻿  ﻿  ﻿  ﻿  continue;﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  var subObj = jsonObj[it];﻿  ﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  var ret = parseJSONAttributes( subObj );
﻿  ﻿  ﻿  ﻿  var attrList = ret.list;
﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  if(subObj!=null && subObj instanceof Object) {
﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  ﻿  if(subObj instanceof Array) {﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result+=parseJSONArray( subObj, it, attrList )﻿  ﻿  ﻿  ﻿  ﻿  
﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  if (!NET || ret.nonAttr) {
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result+=startTag(subObj, it, attrList, false);
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result+=parseJSONObject(subObj);
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result+=endTag(subObj,it);
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  ﻿  result+=startTag(subObj, it, attrList, true);
﻿  ﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  result+=startTag(subObj, it, attrList, false);
﻿  ﻿  ﻿  ﻿  ﻿  result+=parseJSONTextObject(subObj);
﻿  ﻿  ﻿  ﻿  ﻿  result+=endTag(subObj,it);
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  result+=parseJSONTextObject(jsonObj);
﻿  ﻿  
﻿  ﻿  return result;
﻿  }
﻿  
﻿  this.parseXmlString = function(xmlDocStr) {
﻿  ﻿  var xmlDoc;
﻿  ﻿  if (window.DOMParser) {
﻿  ﻿  ﻿  var parser=new window.DOMParser();﻿  ﻿  ﻿  
﻿  ﻿  ﻿  xmlDoc = parser.parseFromString( xmlDocStr, "text/xml" );
﻿  ﻿  }
﻿  ﻿  else {
﻿  ﻿  ﻿  // IE :(
﻿  ﻿  ﻿  if(xmlDocStr.indexOf("<?")==0) {
﻿  ﻿  ﻿  ﻿  xmlDocStr = xmlDocStr.substr( xmlDocStr.indexOf("?>") + 2 );
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
﻿  ﻿  ﻿  xmlDoc.async="false";
﻿  ﻿  ﻿  xmlDoc.loadXML(xmlDocStr);
﻿  ﻿  }
﻿  ﻿  return xmlDoc;
﻿  }

﻿  this.xml2json = function (xmlDoc) {
﻿  ﻿  return parseDOMChildren ( xmlDoc );
﻿  }
﻿  
﻿  this.xml_str2json = function (xmlDocStr) {
﻿  ﻿  var xmlDoc = this.parseXmlString(xmlDocStr);﻿  
﻿  ﻿  return this.xml2json(xmlDoc);
﻿  }

﻿  this.json2xml_str = function (jsonObj) {
﻿  ﻿  return parseJSONObject ( jsonObj );
﻿  }

﻿  this.json2xml = function (jsonObj) {
﻿  ﻿  var xmlDocStr = this.json2xml_str (jsonObj);
﻿  ﻿  return this.parseXmlString(xmlDocStr);
﻿  }

﻿  this.setQuot = function(val) {
﻿  ﻿  if ((/^(s(ingle)?|'|1)$/i).test(val)) {
﻿  ﻿  ﻿  quot = 'single';
﻿  ﻿  }
﻿  ﻿  else if ((/^(d(ouble)?|"|2)$/i).test(val)) {
﻿  ﻿  ﻿  quot = 'double';
﻿  ﻿  }
﻿  }
﻿  this.getQuot = function() {
﻿  ﻿  return quot;
﻿  }

﻿  this.setNET = function(val) {
﻿  ﻿  NET = !!val;
﻿  }
﻿  this.getNET = function() {
﻿  ﻿  return NET;
﻿  }
};

$.extend({
    x2js: new X2JS()
});
})(jQuery);
